package com.powerunion.datacollection.web.struts;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.powerunion.datacollection.service.ColumnManager;
import com.powerunion.datacollection.service.FormGroupHeaderManager;
import com.powerunion.datacollection.transaction.Transaction;
import com.powerunion.datacollection.transaction.TransactionHelper;
import com.powerunion.util.log.LogHelper;

public class ColumnAction extends BaseAction {

	private static final Logger logger = LogHelper.getInstance().getLogger(ColumnAction.class);
	private  ColumnManager columnManager = null;
	
	public ColumnAction() {
		super();
		this.columnManager = new ColumnManager();
	}
	
	public void finalize() throws Throwable {
		this.columnManager = null;
		super.finalize();
	}
	
	public ActionForward save (ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response)
	        throws Exception {
		
		String saveStr = request.getParameter("formData");
		String formId = request.getParameter("formId");

		Transaction transaction = null;
		try {
			transaction = TransactionHelper.getTransaction();
			transaction.begin();
			logger.debug("指标报送区列定义信息:"+saveStr);
			
			boolean flag = this.columnManager.save(saveStr,formId);
			
	        if(flag) {
	        	response.getWriter().write("{success:true}");
	        }else{
	        	response.getWriter().write("{failure:true}");
	        }
			
			transaction.commit();
		} catch (Exception e) {
			logger.error("", e);
			try {
				transaction.rollback();
			} catch (Exception e1) {
				logger.error("", e1);
				throw e;
			} finally {
				transaction = null;
				response.getWriter().write("{failure:true}");
			}
		}

		return null;
	}
	
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String formId = request.getParameter("formId");
		String result = this.columnManager.getColumns(formId);
		logger.debug("columns:"+result);
		response.setContentType("text/json;charset=utf-8");
		try{
			response.getWriter().print(result);
		}catch(Exception e) {
			logger.error("输出json失败",e);
			return null;
		}
		return null;
	}
	
	public ActionForward save4maintblColumn (ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response)
	        throws Exception {
		//列头嵌套定义信息
		String headerInfo = request.getParameter("formData");
		String formId = request.getParameter("formId");
		//最底层列信息
		String columnInfo = request.getParameter("columnInfo");
		//整个列的定义信息
		String definitionInfo = request.getParameter("definitionInfo");

		Transaction transaction = null;
		try {
			transaction = TransactionHelper.getTransaction();
			transaction.begin();
			logger.debug("从表列定义信息:"+headerInfo);
			logger.debug("从表列定义文件信息:"+definitionInfo);
			boolean flag = false;
			this.columnManager.save(columnInfo, formId);
			this.columnManager.save4maintblColumn(headerInfo,formId);
			FormGroupHeaderManager fghm = new FormGroupHeaderManager();
			fghm.save(definitionInfo, formId);
			flag = true;
	        if(flag) {
	        	this.renderSuccssJson(response);
	        }else{
	        	this.renderFAILUREJson(response);
	        }
			
			transaction.commit();
		} catch (Exception e) {
			logger.error("", e);
			try {
				transaction.rollback();
			} catch (Exception e1) {
				logger.error("", e1);
				throw e;
			} finally {
				transaction = null;
				this.renderFAILUREJson(response);
			}
		}

		return null;
	}
	
	public ActionForward getTree(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String formId = request.getParameter("formId");
		response.setContentType("text/json;charset=utf-8");
		JSONArray jsonArray = this.columnManager.getSubFormColumns(formId);
		logger.debug("formId:"+formId+"||getSubFormColumnInfo:"+jsonArray);
		try {
			response.getWriter().print(jsonArray);
		} catch (IOException e) {
			logger.error("输出json失败",e);
			return null;
		}
		return null;
	}
	
	public ActionForward getGroupHeader(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String formId = request.getParameter("formId");
		FormGroupHeaderManager fhm = new FormGroupHeaderManager();
		String result = fhm.getGroupHeaderInfo(formId);
		response.setContentType("text/json;charset=utf-8");
		if(StringUtils.isBlank(result)){
			result = "{data:[]}";
		}
		logger.debug("从表列头嵌套定义信息:"+result);
		try {
			response.getWriter().print(result);
		} catch (IOException e) {
			logger.error("输出json失败",e);
			return null;
		}
		return null;
	}
	
	
}
